package com.meilai.project.controller.web.workflow;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.entity.workflow.FormModule;
import com.meilai.project.entity.workflow.WorkFlowDefault;
import com.meilai.project.service.workflow.FormModuleService;
import com.meilai.project.service.workflow.WorkFlowDefaultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "模板表操作", description = "模板表相关")

@RestController
@RequestMapping("api/web/form_module")
public class FormModuleController {
    @Autowired
    private FormModuleService service;
    @Autowired
    private WorkFlowDefaultService workFlowDefaultService;

    @GetMapping("getList")
    @ApiOperation(value = "获取现有流程列表")
    public CommonResult getList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                @RequestParam(name = "page_size",defaultValue = "10") Integer page_size,
                                @RequestParam(name = "name", required = false) String name,
                                @RequestParam(name = "status", required = false) Integer status,
                                @RequestParam(name = "flow_type", required = false) Integer flow_type){
        return new CommonResult().success(service.selectPageList(page, page_size,name,status,flow_type));
    }

    @GetMapping("getFlowType")
    @ApiOperation(value = "获取流程类型")
    public CommonResult getFlowType(){
        return new CommonResult().success(service.getFlowTypeList());
    }

    @GetMapping("form_module_search")
    @ApiOperation(value = "模板列表")
    public CommonResult selectModule(){
        return new CommonResult().success(service.list(new QueryWrapper<FormModule>().isNull("deleted_at").eq("status",1)));
    }

    @GetMapping("getAllUsersData")
    @ApiOperation(value = "所有用户")
    public CommonResult getAllUserData(){
        return new CommonResult().success(service.getAllUserData());
    }

    @GetMapping("getAllDepartmentsData")
    @ApiOperation(value = "所有部门")
    public CommonResult getAllDepartmentData(){
        return new CommonResult().success(service.getAllDepartmentData());
    }

    @GetMapping("getAllJobsData")
    @ApiOperation(value = "所有职位")
    public CommonResult getAllJobData(){
        return new CommonResult().success(service.getAllJobData());
    }

    @GetMapping("getAllDepJobsData")
    @ApiOperation(value = "所有部门职位")
    public CommonResult getAllDepJobData(){
        return new CommonResult().success(service.getAllDepJobData());
    }


    @GetMapping("getAllCanEditOptions")
    @ApiOperation(value = "模板的可选编辑项Json")
    public CommonResult getAllCanEditOptions(Integer form_module_id){
        return new CommonResult().success(service.getEditOptions(form_module_id));
    }

    @GetMapping("getConnectSetData")
    @ApiOperation(value = "模板的条件配置项Json")
    public CommonResult getConnectSetData(Integer form_module_id){
        return new CommonResult().success(service.getPropertyJson(form_module_id));
    }

    @PostMapping("save_process")
    @ApiOperation(value = "保存流程")
    public CommonResult saveProcess(@RequestBody WorkFlowDefault data){
        if(service.saveProcess(data)){
            return new CommonResult().success("保存成功");
        }else{
            return new CommonResult().failed("保存失败");
        }
    }

    @GetMapping("checkStatus")
    @ApiOperation(value = "确认是否有已启用的流程类型")
    public CommonResult checkStatus(Integer flow_type){
        return new CommonResult().success(workFlowDefaultService.checkStatus(flow_type));
    }

    @PutMapping("changeStatus")
    @ApiOperation(value = "修改启用状态")
    public CommonResult changeStatus(Integer id,Integer flow_type,Integer status){
        if(workFlowDefaultService.changeStatus(id,flow_type,status)){
            return new CommonResult().success();
        }else{
            return new CommonResult().failed("修改失败");
        }
    }

    @PutMapping("delete")
    @ApiOperation(value = "删除流程")
    public CommonResult delProcess(Long id){
        if(workFlowDefaultService.delProcess(id)){
            return new CommonResult().success("删除成功");
        }else{
            return new CommonResult().failed("删除失败");
        }
    }

    @GetMapping("getByCompanyIdAndFlowType")
    @ApiOperation(value = "获取目前启用流程")
    public CommonResult getByCompanyIdAndFlowType(Integer flow_type){
        return new CommonResult().success(service.getByFlowType(flow_type));
    }

    @GetMapping("getTypeNameAndRouter")
    @ApiOperation(value = "获取类型名称和模板路由")
    public CommonResult getTypeNameAndRouter(){
        return new CommonResult().success(workFlowDefaultService.getTypeNameAndRouter());
    }
}
